package com.yamanyar.weebill.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yamanyar.weebill.dao.RoleDAO;
import com.yamanyar.weebill.domain.Role;
import com.yamanyar.weebill.service.RoleService;

/**
 * Implementation of <code>RoleService</code> interface. Created in Jun 28,
 * 2008.
 * 
 * @see RoleService
 * @author Kaan Yamanyar
 */
@Service
public class RoleServiceImpl implements RoleService {

	private RoleDAO roleDAO;

	@Autowired
	public RoleServiceImpl(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public void createNew(Role role) {
		roleDAO.persist(role);
	}

	public List<Role> findAll() {
		return roleDAO.loadAll();
	}

	public Role findByName(String name) {
		List<Role> result = roleDAO.findByName(name);
		int sizeOfResult = result.size();
		if (sizeOfResult == 0)
			return null;
		// else if (sizeOfResult > 1) {
		// throw new IllegalBusinessState(
		// "There are more then one Role's with the given name.");
		//		}
		return result.get(0);
	}

	public Role getRoleWithID(Long id) {
		return roleDAO.loadById(id);
	}
}
